<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<html:html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title></title>
	<META http-equiv=Pragma content=no-cache>
	<META http-equiv=Cache-Control content=no-cache>
	<META http-equiv=Expires content=0>
	<link rel="stylesheet"
		href="<%=request.getContextPath()%>/resource/css/style.css"
		type="text/css">
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/js/util.js"></script>
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/style/scripts/util.js"></script>
	
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/js/Validator.js"></script>	
	<%--<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/calendar/DCDA_C03.js"></script>--%>
<%------------- -----------------------------------动态加载start----------------------------------------------------------------------%> 		
	<script language="javascript" type="text/javascript"
		src="<%=request.getContextPath()%>/resource/js/AdyhUtil.js"></script>	
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/style/ext/adapter/ext/ext-base.js"></script>
	<script type="text/javascript"
		src="<%=request.getContextPath()%>/style/ext/ext-all.js"></script>	
<%------------------------------------------------动态加载end----------------------------------------------------------------------%> 


	<script language="javascript" type="text/javascript">
		
	//------------------------------------------------动态加载start---------------------------------------------------------------------- 
//动态加载 
function FiltSupplier(obj)
  {  	//
  		var strValue = Jtrim(obj.value);   
		if(strValue==""){//检索条件为空时，不检索
			return ;
		}
		if(event.keyCode==38||event.keyCode==40||event.keyCode==13){
			keyEnter();
			if(numsup>=0&&(event.keyCode==38||event.keyCode==40)){
				document.getElementById("row"+numsup).focus();
				document.getElementById("row"+numsup).scrollIntoView(false);
			}
			return;
		}
  		
		var strId =  obj.name;
		  
		//回显的编码的字段名称
		var strCode = "";
		//回显的名称的字段名称
		var strName = "";
		//相应的div的名称
		var strObj = "rtnList";
		
		strTextName=strId
		
		//供应商
		if(strId=='supplierName'){
			strCode = "supplierCode";
			strName = "supplierName";
			
		//材料
		}else if(strId=='materialName'){
			strCode = "materialCode";
			strName = "materialName";
		}
		
   		Ext.Ajax.request(
   		{
		url : 'F0405.do' , 
		params : {operationType:'ajax',id:strId,name:strValue},
		method: 'POST',
		success: function ( result, request) 
			{
			var listArray = new Array();
			var obj= Ext.decode(result.responseText) ;
			var objPrompt = document.getElementById(strObj);			
			objPrompt.style.display = "block";		
			objPrompt.innerHTML = "";
			initPosition(strObj);
			listArray = obj.data.list;
			var inn = "<table WIDTH=\"100%\" BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"1\" bgcolor=\"#FFFFFF\" id=\"sup\" class=\"ListBackground\">";
 	        for (i=0; i<listArray.length; i++){
				inn += "<tr class=\"ListRow1\" height=\"20\" id=\"row"+(i)+"\"  onClick=\"javascript:show('"+strObj+"','"+listArray[i][0]+"','"+strCode+"','"+listArray[i][1]+"','"+strName+"')\">";
				inn += "<td>";
				inn += listArray[i][1];
				inn += "</td>";
				inn += "</tr>";
			}
			inn+="</table>";
			
			rownum=i;//获取动态加载的行数
			
			//若检索的结果是空，关闭div
			if(listArray.length==1&&listArray[0][0]==""&&listArray[0][1]==""){
				inn = "";
			}
			
			objPrompt.innerHTML += inn;
			if(rownum>0&&inn!=""){
				document.getElementById("row0").focus();
			}
			FocusTableRow1("sup",0);//鼠标划过变色
			setPosition(strObj,strName);	//设置div的位置	
			document.getElementById(strCode).value="";
			
			},	
		failure: function ( result, request) { 
			alert("<bean:message key="F020106.title"/>");
			} 
		}
		);
  }

//初始化div的位置
  function initPosition(strObj){
	var objPrompt = document.getElementById(strObj);
	objPrompt.style.top=0;
	objPrompt.style.left=0;
  }
  
  //设置div的位置
  function setPosition(strObjPrompt,strObjText){
  	var objPrompt = document.getElementById(strObjPrompt);
	var objText = document.getElementById(strObjText);
	objPrompt.style.top = 20-(objPrompt.offsetTop-objText.offsetTop);
	objPrompt.style.left = objText.offsetLeft-18;
  }
  
  //设置div的位置
  function initColumn(){
  		if(strTextName=='supplierName'){
			strCode = "supplierCode";
			strName = "supplierName";
			
		//材料
		}else if(strTextName=='materialName'){
			strCode = "materialCode";
			strName = "materialName";
		}
  	document.getElementById(strCode).value = "";
	document.getElementById(strName).value = "";
  }

function loseFocus(strObj){
  	if(numsup>-1){//动态加载的div有数据
		var objRow = document.getElementById("row"+numsup);
		if(objRow!=null){
			objRow.fireEvent("onClick");
		}
	}else{//动态加载的div无数据
		//alert(document.getElementById(strObj).scrollHeight);
		if(document.getElementById(strObj).scrollHeight>240){
			return;
		}
		col(document.getElementById(strObj));
	}
  }
//------------------------------------------------动态加载end---------------------------------------------------------------------- 


  /**保存*/
   function ShowAlert()
  {	
	ResetMe();
	col(document.getElementById("rtnList"));
	//col(document.getElementById("material"));	
	var materialCode = document.getElementById("materialCode").value;
	var materialName = document.getElementById("materialName").value;
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "block";
    objPrompt.innerHTML = "";
	if(materialCode.length==0||materialName.length==0){
		if(materialCode.length==0){
			objPrompt.innerHTML += "<li><bean:message key="adyh.materialCode"/> <bean:message key="adyh.error.isnull"/>！";
			document.getElementById("materialCode").className = "MyControlError";
		}
		if(materialName.length==0){
			objPrompt.innerHTML += "<li><bean:message key="adyh.materialName"/> <bean:message key="adyh.error.isnull"/>！";
			document.getElementById("materialName").className = "MyControlError";
		}
	}else{
		flag = 0;
		//校验是否有重复子项存在
		objPrompt.style.display = "none";		
		if(CheckRepeat()==false){
			return false;
		}
		//增加一行值
		addRow();			
	}	
  }
/**重置*/
function ResetMe()
{
    document.getElementById("materialCode").className = "MyText";
	
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "none";
	objPrompt.innerHTML = "";
}

function ShowFormTitle(strTitle)
{
	document.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"height:26px; background-position: left bottom; background-image: url(../../resource/image/FormTitle_A_4.gif); background-repeat: repeat-x;\">");
	document.write("  <tr >");
	document.write("	<td style=\"width:5px; background-position: left bottom; background-image: url(../../resource/image/FormTitle_A_1.gif); background-repeat: no-repeat;\">&nbsp;&nbsp;&nbsp;</td>");
	document.write("	<td class=\"FormTitle\" style=\"width:100%; padding-left:5px; padding-top:3px; background-position: left bottom; background-image: url(../../resource/image/FormTitle_A_2.gif); background-repeat: no-repeat;\">");
	document.write("		<span id=\"spanFormTitle\" class=\"SpanTitle\">"+ strTitle +"</span>");
	document.write("	</td>");
	document.write("	<td style=\"width:5px; background-position: right bottom; background-image: url(../../resource/image/FormTitle_A_3.gif); background-repeat: no-repeat;\">&nbsp;&nbsp;&nbsp;</td>");
    document.write("  </tr>");
	document.write("</table>");
}

//显示隐藏区域
function AddOnce(strDivId)
{
	var objDiv = document.getElementById(strDivId);
	var objInnerDivs = objDiv.getElementsByTagName("div");
            for (var i=0; i<objInnerDivs.length; i++)
			{
			    if(objInnerDivs[i].className == "DivTail")
				{
				    objInnerDivs[i].style.display = "block";
				}
				if(objInnerDivs[i].className == "DivWrap")
				{
				    objInnerDivs[i].style.display = "block";
				}
			}
}
//隐藏被置显示区域,并清空数据

function DelOnce(strDivId)
{
	var objDiv = document.getElementById(strDivId);
	var objInnerDivs = objDiv.getElementsByTagName("div");
            for (var i=0; i<objInnerDivs.length; i++)
			{
			    if(objInnerDivs[i].className == "DivTail")
				{
				    objInnerDivs[i].style.display = "none";
				}
				if(objInnerDivs[i].className == "DivWrap")
				{
				    objInnerDivs[i].style.display = "none";
				}
			}
}
//增加主体信息并锁定

function AddMain()
  {	
	ResetMain();
	var supplierCode = document.getElementById("supplierCode").value;
	var supplierName = document.getElementById("supplierName").value;
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "block";
    objPrompt.innerHTML = "";
	if(supplierCode.length==0||supplierName.length==0){
		if(supplierCode.length==0){
			objPrompt.innerHTML += "<li><bean:message key="F0404.supplierCode"/> <bean:message key="adyh.error.isnull"/>！";
			document.getElementById("supplierCode").className = "MyControlError";
		}
		if(supplierName.length==0){
			objPrompt.innerHTML += "<li><bean:message key="F0404.supplierName"/> <bean:message key="adyh.error.isnull"/>！";
			document.getElementById("supplierName").className = "MyControlError";
			//document.getElementById("supplierName").style.position="relative";
		}
						
	}else{	
		objPrompt.style.display = "none";	
		//将输入框置为disable
		//document.getElementById("supplierCode").disabled="disabled";
		document.getElementById("supplierName").disabled="disabled";
		//显示隐藏区域
		AddOnce('divDlock_2');
		//切换按钮
		document.getElementById("addDetail").style.display="none";
		document.getElementById("delDetail").style.display="";
	}
	col(document.getElementById("rtnList"));	
	//setPosition(document.getElementById("supplier"),document.getElementById("supplierName"));
  }
/**重置主体信息*/
function ResetMain()
{
    document.getElementById("supplierName").className = "MyText";
	//document.getElementById("supplierName").style.position="relative";
	var objPrompt = document.getElementById("divPrompt");
    objPrompt.style.display = "none";
	objPrompt.innerHTML = "";
}
//删除主体信息并解除锁定、清空隐藏区域信息

function ClearAll()
  {			
		//将输入框置为enable并清空值

		document.getElementById("supplierCode").disabled="";
		document.getElementById("supplierName").disabled="";
		//清空详细信息的输入条件

		ClearIn();
		//清空隐藏区域信息
		delRows();
		//隐藏区域	
		DelOnce('divDlock_2');
		//切换按钮
		document.getElementById("addDetail").style.display="";
		document.getElementById("delDetail").style.display="none";
		document.getElementById("material").style.top = 0;
  }
//清空详细信息的输入条件

function ClearIn()
  {
		document.getElementById("materialCode").value="";
		document.getElementById("materialName").value="";
  } 
function onSave(){		
	if(count==0){
		alert("<bean:message key="F020106.title"/>");
	}else{
		document.getElementById("supplierCode").disabled="";
		document.getElementById("supplierName").disabled="";
		document.getElementById("id").value = idstr;		
		var operationType = "insert";
		operation(operationType);
	}
  }
  
  
//校验是否输入了重复的子项
function CheckRepeat()
  {
	var materialCode = document.getElementById("materialCode").value;		
		for(i=0;i<count;i++){			
			if(materialCode==document.getElementsByName("material_Code")[i].value){
				alert("<bean:message key="F020106.title"/>");
				return false;
			}			
		}
	return true;
  }
</script>
</head>

<body>
	<html:form method="post" action="/pages/F04/F0405.do">
		<html:hidden property="operationType" />
		<html:hidden property="id" />
		<html:hidden property="supplierCode" />
		<html:hidden property="language"/>
		<html:hidden property="materialCode" />
		<html:hidden property="errorFlag" />
		<!--Begin：表单标题-->
		<div class="DivBlock">
			<script language="javascript" type="text/javascript">
		ShowFormTitle("<bean:message key="F0405.title"/>");
	</script>
		</div>
		<!--End：表单标题-->
		
		<!--Begin：表单主体区块-->
		<div class="DivBlock">
			<div class="DivWrap" style="display: block">
				<TABLE WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0">
					<TR>
						<TD ALIGN="right" NOWRAP width="25%">
							<!--供应商名称-->
							<bean:message key="F0404.supplierName"/>
						</TD>
						<TD ALIGN="left" NOWRAP width="40%">
						<%------------- -----------------------------------动态加载start----------------------------------------------------------------------%>
							<html:text property="supplierName" styleClass="MyText" maxlength="50" onkeyup="FiltSupplier(this)" ondblclick="FiltSupplier(this)" style="position:relative" onblur="loseFocus('rtnList')"/>	
						<%------------------------------------------------动态加载end----------------------------------------------------------------------%>						
						</TD>
						<TD ALIGN="left" NOWRAP>
							<input type="button" name="addDetail" class="MyButton"
								onClick="AddMain()" value="<bean:message key="common.add"/>">
				
							<input type="button" name="delDetail" class="MyButton" style="display:none"
								onClick="ClearAll()" value="<bean:message key="common.delete"/>">
						</TD>
					</TR>
				</TABLE>
			</div>
		</div>
		<!--End：表单主体区块-->
		
		<!--Begin：表单主体区块2-->
		<div id="divDlock_2" class="DivBlock">
			<div class="DivWrap" style="display:none">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td>
							<strong><bean:message key="F0405Detail.title" />
							</strong>
						</td>
					</tr>
				</table>
			</div>
			<div class="DivTail" style="display:none">
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<TR>
						<TD ALIGN="right" NOWRAP width="25%">
							<!--材料名称-->
							<bean:message key="adyh.materialName" />
						</TD>
						<TD ALIGN="left" NOWRAP width="40%">
						<%------------- -----------------------------------动态加载start----------------------------------------------------------------------%>
							<html:text property="materialName" styleClass="MyText" onkeyup="FiltSupplier(this)" ondblclick="FiltSupplier(this)" style="position:relative" maxlength="50" onblur="loseFocus('rtnList')" />
						<%------------------------------------------------动态加载end----------------------------------------------------------------------%>
						</TD>
						<TD ALIGN="left" NOWRAP>
							<input type="button" name="add" class="MyButton"
								onClick="ShowAlert();ClearIn();" value="<bean:message key="common.add"/>">
						</TD>
					</TR>
				</table>
			</div>								
			<div class="DivTail" style="display:none">
				<table id="dytb" width="100%" cellpadding="1" cellspacing="1"
					border="0" rules="none"  class="ListBackground">
					<tbody id="tb1">
					<TR class="ListTitle" id="title">
						<TD ALIGN="center" NOWRAP>
							<!--工艺阶段-->
							<bean:message key="adyh.materialCode" />							
						</TD>
						<TD ALIGN="center" NOWRAP>
							<!--剩余天数-->
							<bean:message key="adyh.materialName" />							
						</TD>
						<TD ALIGN="center" NOWRAP>
							<!--操作-->
							<bean:message key="common.operate" />							
						</TD>
					</TR>					
					</tbody>
				</table>
			</div>						
		</div>
		<!--End：表单主体区块2-->			
		
		<!--Begin：表单按钮及提示区-->
		<div class="DivBlock">
			<div class="DivButton">
				<input type="button" name="Submit1" class="MyButton"
					onClick="onSave()" value="<bean:message key="common.save"/>">
				<input type="button" name="Submit2" class="MyButton"
					value="<bean:message key="common.back"/>"
					onClick="javascript:operation('back');">
			</div>			
			<div class="DivPrompt" id="divPrompt" style="display: none; position:relative">
			</div>
		</div>
		<%------------- -----------------------------------动态加载start----------------------------------------------------------------------%>
		<div id="rtnList" style="display: none; position:relative; overflow:auto; width:130; height:240" onmouseleave="col(this);initColumn()"  >
		</div>
		<%------------------------------------------------动态加载end----------------------------------------------------------------------%>
		<!--End：表单按钮及提示区-->
	</html:form>
</body>
</html:html>


<script language="javascript" type="text/javascript">
//setPosition(document.getElementById("supplier"),document.getElementById("supplierName"));
InitForm();
//动态增加行
var itable = document.getElementById("tb1");//表格
var key = itable.rows.length;//id起始值

var count = 0;
var idstr = "";
function addRow(){//添加表格的一行	
	var currRowNum = itable.rows.length;//当前位置
		
	var iTR;//插入的行

	var iTD;//插入的列
		
	iTR=itable.insertRow(currRowNum);
	
	idstr = idstr+(key+",");//组装id串(1,2,3....)
	
	iTR.id="tr"+key;//行id
	iTR.className="ListRow1";		
	
	iTD=iTR.insertCell(0);	
	var id1=document.getElementById("materialCode").value;
	iTD.innerHTML="<td><input id=\"material_code"+key+"\" name=\"material_code\" value=\""+id1+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\" /></td>";
			
	iTD=iTR.insertCell(1);			
	var id2=document.getElementById("materialName").value;
	iTD.innerHTML="<td><input id=\"material_Name"+key+"\" name=\"material_Name\" value=\""+id2+"\" styleClass=\"MyText\" style=\"width:100%;text-align:center;background:none;border:0pt\" readonly=\"true\" /></td>";
	
	iTD=iTR.insertCell(2);	
	iTD.innerHTML="<td nowrap align=\"center\"><button onclick=\"delRow(tr"+key+")\" class=\"Mybutton\"\"><bean:message key="common.delete"/></button></td>";
	//iTD.className="list_td";	
	
	key++;
	count++;
		
}

function delRow(str)
{
	if(confirm("<bean:message key="common.alert11"/>")){
	
	sIndex=str.rowIndex;
 	if(sIndex=='')
    	return;
 
	itable.deleteRow(sIndex);
	count--;
	replaceIdStr(str.id);
	return true;
	}else{
		return false;
	}
}

function delRows()
{			
 	for(i=0;i<count;) {   	
   	itable.deleteRow(count);
   	count--;
  	} 	 		  	
}

function replaceIdStr(str)
{
	if(str=="")
	{
		return ;
	}else
	{
		str = str.replace("tr","");//去掉id中的“tr”部分tr2-->2
		str = str+",";
		
		idstr = idstr.replace(str,"");
		
	}
}
</script>